import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/entities/account_entity.dart';
import 'package:finance_app/src/utils/language_utils.dart';
import 'package:finance_app/src/widgets/button/normal_button.dart';
import 'package:finance_app/src/widgets/select/normal_check_box.dart';
import 'package:flutter/material.dart';

import 'default_bottom_sheet.dart';

/// 交易订单筛选
showFilterPaymentBottomSheet(
  BuildContext context, {
  List<String> defaultFilter,
  List<String> selectFilter,
  List<AccountEntity> banks,
  List<AccountEntity> selectBank,
  bool isHideBankSelect,
}) async {
  List<String> _currentSelectFilter = selectFilter;
  List<AccountEntity> _currentSelectBank = selectBank;
  GlobalKey<FilterBottomSheetState> _key = GlobalKey();

  await showDefaultBottomSheet(
    context,
    child: FilterBottomSheet(
      key: _key,
      value: selectFilter,
      selectCallBack: (data) {
        _currentSelectFilter = data['select'];
        _currentSelectBank = data['bankSelect'];
      },
      isHideBankSelect: isHideBankSelect,
      bankValue: selectBank,
      banks: banks,
      group: defaultFilter ??
          [
            'Money-In',
            'Money-Out',
            'Account Transfer',
          ],
    ),
    leading: DefaultBottomSheetLeadingButton(
      title: S.current.reset,
      onTap: () {
        _key.currentState.onClear();
      },
    ),
    title: S.current.filter_transaction,
    isScrollControlled: true,
  );
  return {
    'select': _currentSelectFilter,
    'bankSelect': _currentSelectBank,
  };
}

class FilterBottomSheet extends StatefulWidget {
  final List<String> value;
  final List<AccountEntity> bankValue;
  final List<AccountEntity> banks;
  final ValueChanged<Map<String, dynamic>> selectCallBack;
  final List<String> group;
  final isHideBankSelect;

  const FilterBottomSheet(
      {Key key,
      this.value,
      this.selectCallBack,
      this.group,
      this.bankValue,
      this.banks,
      this.isHideBankSelect})
      : super(key: key);

  @override
  FilterBottomSheetState createState() => FilterBottomSheetState();
}

class FilterBottomSheetState extends State<FilterBottomSheet> {
  List<String> filterList = [];

  List<String> currentSelect = [];

  List<AccountEntity> currentBankSelect = [];

  bool get enable => currentSelect.isNotEmpty || currentBankSelect.isNotEmpty;

  @override
  void initState() {
    super.initState();
    filterList = widget.group ?? [];
    currentSelect = widget.value;
    currentBankSelect = widget.bankValue;
  }

  void onChangeSalesTrend(String value) {
    if (currentSelect.contains(value)) {
      currentSelect.remove(value);
    } else {
      currentSelect.add(value);
    }
    setState(() {});
  }

  void _onApply() {
    widget.selectCallBack?.call({
      'select': currentSelect,
      'bankSelect': currentBankSelect,
    });
    Navigator.of(context).pop();
  }

  void onClear() {
    setState(() {
      currentSelect.clear();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        SizedBox(
          height: getDp(17),
        ),
        _buildTop(S.current.by_type),
        SizedBox(
          height: getDp(17),
        ),
        Padding(
          padding: EdgeInsets.symmetric(
            horizontal: getDp(30),
          ),
          child: Wrap(
            crossAxisAlignment: WrapCrossAlignment.center,
            alignment: WrapAlignment.start,
            children: filterList
                .map((e) => PressedOpacityButton(
                      onPressed: () => onChangeSalesTrend(e),
                      padding: EdgeInsets.symmetric(
                        vertical: getDp(10),
                      ),
                      child: NormalCheckBox(
                        title: LanguageUtils.translateFilter(e),
                        minWidth: getDp(140),
                        isSelect: currentSelect.contains(e),
                      ),
                    ))
                .toList(),
          ),
        ),
        if (widget.isHideBankSelect != true)
          _buildTop(S.current.by_bank_account),
        if (widget.isHideBankSelect != true)
          SizedBox(
            height: getDp(17),
          ),
        if (widget.isHideBankSelect != true && widget.banks != null)
          Padding(
            padding: EdgeInsets.symmetric(
              horizontal: getDp(30),
            ),
            child: Wrap(
              crossAxisAlignment: WrapCrossAlignment.center,
              alignment: WrapAlignment.start,
              children: widget.banks
                  .map((e) => PressedOpacityButton(
                        onPressed: () => onChangeSalesBank(e),
                        padding: EdgeInsets.symmetric(
                          vertical: getDp(10),
                        ),
                        child: NormalCheckBox(
                          title: '${e.bankName} ${e.accountNumber ?? ''}',
                          minWidth: getDp(140),
                          isSelect: currentBankSelect.contains(e),
                        ),
                      ))
                  .toList(),
            ),
          ),
        SizedBox(
          height: getDp(30),
        ),
        NormalButton(
          onTap: enable ? _onApply : null,
          title: S.current.apply,
        ),
      ],
    );
  }

  Widget _buildTop(String title) => Container(
        height: getDp(32),
        color: aceWhiteColor,
        padding: EdgeInsets.symmetric(horizontal: getDp(24)),
        alignment: Alignment.centerLeft,
        child: Text(
          title,
          style: TextStyle(
            color: aceDarkGrayColor,
            fontSize: getSp(16),
            fontWeight: FontWeight.w400,
            height: 22 / 16,
          ),
        ),
      );

  void onChangeSalesBank(AccountEntity e) {
    setState(() {
      if (currentBankSelect.contains(e)) {
        currentBankSelect.remove(e);
      } else {
        currentBankSelect.add(e);
      }
    });
  }
}
